{% load i18n %}
<div class="position-relative">
<div id="scenariotoast" class="toast top-0 start-50 translate-middle-x"
  data-bs-delay="3000" role="alert" aria-live="assertive" aria-atomic="true">
  <div class="toast-body bg-success">
  <span class="text-white"></span>
  </div>
</div>
</div>
<div class="table-responsive">
<table id="scenarios" style="width:100%" class="w-100 ">
  <tr>
      <td colspan='5' style="padding-left:15px">
        <p>{% blocktrans %}You have a number of available scenario databases available.<br>
        You can copy data into a scenario database to create a isolated sandbox for what-if analysis.<br>
        Use the dropdown in the upper right corner of the screen to select which scenario you're working in.
        {% endblocktrans %}</p>
      </td>
  </tr>
  <tr>
    <th style="padding:5px 10px 5px 10px; text-align: center">{% trans 'scenario'|capfirst %}</th>
    <th style="padding:5px 10px 5px 10px; text-align: center">{% trans 'action'|capfirst %}</th>
    <th style="padding:5px 10px 5px 10px; text-align: center">
      <span data-bs-toggle="tooltip" data-bs-placement="top" data-bs-html="true"
        data-bs-title="{% trans "<b>In use</b>: Contains data<br><b>Free</b>: Available to copy data into<br><b>Busy</b>: Data copy in progress" %}">
      {% trans 'status'|capfirst %}
      <span class="fa fa-question-circle"></span>
      </span>
    </th>
    <th style="padding:5px 10px 5px 10px; text-align: center">
      <span data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="{% trans "Label shown in the scenario dropdown list" %}">
      {% trans 'label'|capfirst %}
      <span class="fa fa-question-circle"></span>
      </span></th>
    {% if THEMES|length > 1 %}
    <th style="padding:5px 10px 5px 10px; text-align: center">
      <span data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="{% trans "Theme for this scenario" %}">
      {% trans 'theme'|capfirst %}
      <span class="fa fa-question-circle"></span>
      </span>
    </th>
    {% endif %}
  </tr>
  {% for j in scenarios %}
  {% if j.status == "Free" or j.name in active_scenarios %}
  <tr>
    <td style="padding:5px">
      <strong>{{j.name|capfirst}}</strong>
    </td>
    <td style="padding:5px 10px 5px 10px">
       {% if j.name != DEFAULT_DB_ALIAS and j.status == "Free" and perms.common.copy_scenario %}
       <div class="btn-group btn-block">
       <button class="btn btn-block btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">
         {% trans 'manage'|capfirst %}&nbsp;<span class="caret"></span>
       </button>
       <ul class="dropdown-menu" role="menu">
         <li><a class="dropdown-item scenariocopy" href="#" data-source="{{ current_database }}" data-bs-target="{{ j.name }}">
           {% trans "Copy from" %} {{ current_database }}
         </a></li>
         {% for dump in dumps %}
         <li><a class="dropdown-item scenariocopy" href="#" dumpfile="{{ dump }}" data-source="{{ current_database }}" data-bs-target="{{ j.name }}">{% trans "Restore from backup" %} {{ dump }}</a></li>
         {% endfor%}
       </ul>
       </div>
       {% elif j.name != DEFAULT_DB_ALIAS and j.status == 'In use'%}
       {% if j.name in active_scenarios  %}
       <div class="btn-group btn-block">
       <button class="btn btn-block btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">
         {% trans 'manage'|capfirst %}&nbsp;<span class="caret"></span>
       </button>
       <ul class="dropdown-menu" role="menu">
         <li><a class="dropdown-item scenariorelease" href="#" data-bs-target="{{ j.name }}">
         {% trans "Release: You will lose ALL data in this scenario!" %}
         </a></li>
         {% if current_database == j.name %}
         <li><a class="dropdown-item scenariopromote" href="#" data-source="{{ j.name }}" data-bs-target="{{ DEFAULT_DB_ALIAS }}" >
         {% trans "Promote: All data will be copied to Production" %}
         </a></li>
         {% endif %}
       </ul>
       </div>
       {% endif %}
       {% endif %}
    </td>
    <td style="padding:5px 10px 5px 10px; text-align: center">{% trans j.status|lower|capfirst %}</td>
    <td style="padding:5px 10px 5px 10px">
      <input class="form-control scenariolabel" type="text" data-bs-target="{{ j.name }}"
      value="{{j.tag|escape}}">
    </td>
    {% if THEMES|length > 1 %}
    <td style="padding:5px 10px 5px 10px; text-align: center">
      <div id="themedropdown" class="dropdown" data-bs-target="{{ j.name }}">
        <button class="form-control dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
          {% for sc, th in request.user.scenario_themes.items %}
            {% if sc == j.name %}{{ th }}{% endif %}
          {% endfor %}
        </button>
        <ul class="dropdown-menu w-100">
          <li><a class="scenario_theme dropdown-item" data-theme="">{% trans 'default' %}</a></li>
          {% for k in THEMES %}
            <li><a class="scenario_theme dropdown-item" data-theme="{{ k }}">{{ k }}</a></li>
          {% endfor %}
        </ul>
      </div>
    </td>
    {% endif %}
  </tr>
  {% endif %}
  {% endfor %}
</table>
</div>

<script>
$(".scenariorelease").on("click", function(event) {
  event.preventDefault();
  var releasedScenario = $(this).attr("data-bs-target");
  var target = "/" + releasedScenario;
  if (target == "/default")
    target = "";
  $.ajax({
   url: target + "/execute/launch/scenario_copy/",
   type: 'POST',
   data: {release: 1, database:releasedScenario},   
   success: function() {
     $("#scenariotoast span").text("Launched releasing task");
     bootstrap.Toast.getOrCreateInstance($("#scenariotoast")[0]).show();
     if (target == url_prefix) window.location.href = "/"; },
   fail: function() {
    console.log("FAILED");
   }});   
});
$(".scenariopromote").on("click", function(event) {
  event.preventDefault();
  var source = "/" + $(this).attr("data-source");
  $.ajax({
   url: source + "/execute/launch/scenario_copy/",
   type: 'POST',
   data: {
     promote: 1,
     source: $(this).attr("data-source"),
     destination: $(this).attr("data-bs-target")
     },
   success: function() {
     $("#scenariotoast span").text("Launched promotion task");
     bootstrap.Toast.getOrCreateInstance($("#scenariotoast")[0]).show();
   },
   fail: function() {
    console.log("FAILED");
   }});
});
$(".scenariocopy").on("click", function(event) {
  event.preventDefault();
  var source = "/" + $(this).attr("data-source");
  if (source == "/default")
    source = "";
  $.ajax({
   url: source + "/execute/launch/scenario_copy/",
   type: 'POST',
   data: {
     copy: 1,
     source: $(this).attr("data-source"),
     destination: $(this).attr("data-bs-target"),
     dumpfile: $(this).attr("dumpfile")
     },
   success: function() {
     $("#scenariotoast span").text("Launched copy task");
     bootstrap.Toast.getOrCreateInstance($("#scenariotoast")[0]).show();
   }});
});
$(".scenariolabel").on("change", function(event) {
  event.preventDefault();
  $.ajax({
   url: "/default/execute/launch/scenario_copy/",
   type: 'POST',
   data: {
     update: 1,
     scenario: $(this).attr("data-bs-target"),
     description: $(this).val()
     },
   });
});
$(".scenario_theme").click(function() {
  event.preventDefault();
  var b = $(this);
  $.ajax({
   url: "/default/execute/launch/scenario_copy/",
   type: 'POST',
   data: {
     scenario: b.closest("div").attr("data-bs-target"),
     theme: b.attr("data-theme")
     },
   success: function() {
     b.closest("div").children("button").text(b.attr("data-theme"));
     if (database == b.closest("div").attr("data-bs-target"))
       location.reload();
    }
   });
});
</script>
